package com.cqeec.waimai4.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqeec.waimai4.entity.Employee;
import com.cqeec.waimai4.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 查询employee表的全部数据
     */
    @PostMapping("/getList")
    public List<Employee> getList(){
        return employeeService.list();
    }
    //根据id查询
    @GetMapping("/getById/{id}")
    public Employee getById(@PathVariable("id") Integer id){
       Employee employee= employeeService.getById(id);
//       employeeService.
        return employee;
    }
    //条件查询，查询电话中有237的所有数据
    @GetMapping("/getByPhone")
    public List<Employee> getByPhone(){
        //实例化条件构造器
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        //添加条件
        //wrapper.条件的关键字（获取表中字段的名字，字段对应的值）
        //Employee::getPhone ==> phone
        wrapper.like(Employee::getPhone,"237");
        //调用查询方法
        List<Employee> list = employeeService.list(wrapper);
        return list;
    }
    //性别等于1的按照id进行降序排序
    @GetMapping("/getBySex")
    public List<Employee>getBySex(){
        //实例化条件构造器
        LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
        //添加条件
        wrapper.eq(Employee::getSex,1).orderByDesc(Employee::getId);
        //调用查询方法
        List<Employee> list=employeeService.list(wrapper);
        return list;

    }
}
